package com.faxsun.web.security;

import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.faxsun.web.aspect.TraceAsyncHandler;
import com.faxsun.web.endpoint.LiveCommentEndpoint;



public class WebContextListener implements ServletContextListener{
    
    private static Logger logger = LoggerFactory.getLogger(WebContextListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {            
            // shutdown live comment comet executors
            LiveCommentEndpoint.shutdown = true;
            
            // shutdown async trace executors
            TraceAsyncHandler.scheduler.shutdownNow();            
            TraceAsyncHandler.scheduler.awaitTermination(LiveCommentEndpoint.timeoutVal, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            logger.error("pool shutdown timeout or error on jvm exit", e);
        }
        return;
    }
    
}